package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private SpuMapper spuMapper;

    public BrandServiceImpl(){log.debug("创建业务对象：BrandServiceImpl");}

    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO) {
        int count = brandMapper.countByName(brandAddNewDTO.getName());
        if(count>0){
            String message = "添加品牌失败，尝试添加的品牌名称已经被使用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO, brand);
        int rows = brandMapper.insert(brand);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除品牌】的业务，参数：{}", id);
        //查询此id的品牌数据是否存在
        Object queryResult = brandMapper.getStandardById(id);
        if(queryResult == null){
            String message = "删除品牌失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //如果存在“品牌与类别的关联数据”涉及此品牌，则不允许删除
        int count = brandCategoryMapper.countByBrandId(id);
        if(count > 0){
            String message = "删除相册失败，存在品牌与类别的关联数据！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //如果存在SPU关联到此品牌，则不允许删除
        count = spuMapper.countByBrandId(id);
        if(count > 0){
            String message = "删除相册失败，存在SPU关联到此品牌！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        log.debug("即将执行删除数据，参数：{}", id);
        int rows = brandMapper.deleteById(id);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id, 0);
    }

    @Override
    public void updateInfoById(Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【根据id修改品牌】的业务，品牌ID：{}，新数据：{}", id, brandUpdateDTO);
        //查询此id的品牌数据是否存在
        Object queryResult = brandMapper.getStandardById(id);
        if(queryResult == null){
            String message = "修改品牌失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //查询品牌名称是否冲突
        int count = brandMapper.countByNameAndNotId(id, brandUpdateDTO.getName());
        if(count > 0){
            String message = "修改品牌失败，品牌名称已经被别的数据占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandUpdateDTO, brand);
        brand.setId(id);
        log.debug("即将修改的新数据为：{}", brand);
        int rows = brandMapper.update(brand);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询品牌】的业务，参数：{}", id);
        //查询此id的品牌数据是否存在
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if(queryResult == null){
            String message = "查询品牌失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回相册详情：{}", queryResult);
        return queryResult;
    }

    @Override
    public List<BrandListItemVO> list() {
        log.debug("开始处理【查询品牌列表】的业务，参数：无");
        List<BrandListItemVO> list = brandMapper.list();
        return list;
    }

    private void updateEnableById(Long id, Integer enable){
        String[] enableText = {"禁用", "启用"};
        log.debug("开始处理" + enableText[enable] + "品牌的业务，品牌ID：{}，目标状态：{}", id, enable);
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        //数据是否存在
        if(queryResult == null){
            String message = enableText[enable] + "品牌失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //是否状态冲突
        if(queryResult.getEnable().equals(enable)){
            String message = enableText[enable] + "品牌失败，当前品牌已经是" + enableText[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Brand brand = new Brand();
        brand.setId(id);
        brand.setEnable(enable);
        log.debug("即将修改数据，参数：{}", brand);
        int rows = brandMapper.update(brand);
        if (rows != 1) {
            String message = "添加管理员失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

}
